
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.FileUpLoader.DomainService;
using SOEI.Solution.Helper;
using SOEI.Solution.Integratedes.Organization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.Integratedes.ElectronicSignet.DomainService
{
	/// <summary>
	/// 领域服务层一个模块的核心业务逻辑
	///</summary>
	public class PledgeBookManager :DomainServiceBase, IPledgeBookManager
	{
		
		private readonly IRepository<PledgeBook,long> _pledgebookRepository;


		private readonly IAttachManager _attachManager;

		/// <summary>
		/// 【PledgeBook】的构造方法
		/// 通过构造函数注册服务到依赖注入容器中
		///</summary>
		public PledgeBookManager(
			IRepository<PledgeBook, long> pledgebookRepository,
			IAttachManager attachManager
		)	{
			_pledgebookRepository =  pledgebookRepository;
			_attachManager = attachManager;
		}

		#region -------------------------------------------------辅助工具生成---------------------------------------------- 

		/// <summary>
		/// 返回列表查询用
		/// </summary>
		/// <returns></returns>
		public IQueryable<PledgeBook> QueryEntityListAsNoTracking() { 

			var query = _pledgebookRepository.GetAll().AsNoTracking()
						.Select(x => new PledgeBook
						{
						   
							JGBH = x.JGBH,
							PledgeBookModuleID = x.PledgeBookModuleID,
							Remark = x.Remark,
						});
			return query;
		}

		/// <summary>
		/// 【PledgeBook】返回表达式数的实体信息即IQueryable类型
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<PledgeBook> Query()
		{
			return _pledgebookRepository.GetAll();
		}
		/// <summary>
		/// 【PledgeBook】返回即IQueryable类型的实体，不包含EF Core跟踪标记
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<PledgeBook> QueryAsNoTracking()
		{
			return _pledgebookRepository.GetAll().AsNoTracking();
		}
		/// <summary>
		/// 【PledgeBook】根据Id查询实体信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public async Task<PledgeBook> FindByIdAsync(long id)
		{
			var entity = await _pledgebookRepository.GetAsync(id);
			return entity;
		}
		/// <summary>
		/// 【PledgeBook】检查实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns>bool</returns>
		public async Task<bool> IsExistAsync(long id)
		{
			var result = await _pledgebookRepository.GetAll().AnyAsync(a => a.Id == id);
			return result;
		}
		/// <summary>
		/// 【PledgeBook】创建实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<PledgeBook> CreateAsync(PledgeBook entity)
		{
			entity.Id = await _pledgebookRepository.InsertAndGetIdAsync(entity);
			return entity;
		}
		/// <summary>
		/// 【PledgeBook】更新实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task UpdateAsync(PledgeBook entity)
		{
			await _pledgebookRepository.UpdateAsync(entity);
		}
		/// <summary>
		/// 【PledgeBook】删除实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task DeleteAsync(long id)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _pledgebookRepository.DeleteAsync(id);
		}
		/// <summary>
		/// 【PledgeBook】批量删除
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task BatchDelete(List<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _pledgebookRepository.DeleteAsync(a => input.Contains(a.Id));
		}
		#endregion


		#region -------------------------------------------------用户自定义------------------------------------------------

		/// <summary>
		/// 是否存在指定条件的印章实体
		/// </summary>
		/// <param name="JGBH">机构编号</param>
		/// <param name="RYBH">人员编号</param>
		/// <returns></returns>
		public async Task<bool> IsExistAsync(OEconomyOrgani organi)
		{
			var result = await this._pledgebookRepository.GetAll().AnyAsync(a => a.JGBH == organi.Id);
			return result;
		}

		/*请在此扩展领域服务接口*/

		/// <summary>
		///  获取一个承若书实体，如果不存在则会创建一个
		/// </summary>
		/// <param name="organi"></param>
		/// <returns></returns>
		public async Task<PledgeBook> GetCreatePledgeBookAsync(OEconomyOrgani organi)
		{
			// 如果存在获取最早创建
			if (await this.IsExistAsync(organi))
			{
				// 获取最早创建的那条记录
				var PledgeEntity = await this.Query().OrderBy(x => x.CreationTime).Where(a => a.JGBH == organi.Id).FirstOrDefaultAsync();
				//PledgeBookAttachs
				// 获取附件清单
				PledgeEntity.PledgeBookAttachs = await _attachManager.GetAttachByModuleID(PledgeEntity.PledgeBookModuleID);
				return PledgeEntity;
			}
			else {
				var PledgeEntity = EntityHelper.GetNewPledgeBook();
				PledgeEntity = await this.CreateAsync(PledgeEntity);
				PledgeEntity.JGBH = organi.Id;
				return PledgeEntity;
			}
		}

		#endregion









	}
}
